using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Zhaoxi.MSACommerce.OrderService.Infrastructure.Data.Configuration;
using ZhaoXi.MSACommerce.SharedKenel.Messaging;
using ZhaoXi.MSACommerce.SharedKenel.Result;

namespace Zhaoxi.MSACommerce.OrderService.UseCases.Queries;

public record GetOrderQuery(long OrderId) : IQuery<Result<OrderDto>>;

public class GetOrderQueryHandler(OrderDbContext dbContext, IMapper mapper) : IQueryHandler<GetOrderQuery, Result<OrderDto>>
{
    public async Task<Result<OrderDto>> Handle(GetOrderQuery request, CancellationToken cancellationToken)
    {
        var order = await dbContext.Orders.FirstOrDefaultAsync(o  => o.Id == request. OrderId,cancellationToken);
        
        if (order == null) return Result.NotFound();
        
        var orderDto = mapper.Map<OrderDto>(order);
        
        return Result.Success(orderDto);
    }
}